*********************************************************************
***  Figure 7: Effect of ACA Insurance Expansion
	
	*************************
	*** Panel A (First Stage)

	import delimited using "${mypath}/intermediate_csv/govtpolicy02-ACA_eventstudy_FS.csv", clear asdoub
	gen year = real(substr(parm, 1, 4))
	insobs 1 
	replace year = 2013 if missing(year)
	replace estimate = 0 if missing(estimate)
	sort year
	keep if year>2007
		
	#d ;
	tw (connect estimate year, lcolor("8 48 107") mcolor("8 48 107") lpattern(solid)) 
		(rcap min95 max95 year, lcolor("8 48 107") lpatter(dash)), 
		xtitle("Year", size(medium)) 
		ylabel(, labsize(medium))
		xlabel(2008(1)2017, labsize(medium) nogrid ) 
		legend(off) 
		graphregion(color(white)) 
		yline(0, lcolor(gs8) lpattern(dash) lwidth(thin)) 
		xline(2014, lwidth(5.5) lc(gs12*.4) lpattern(solid))
		xsize(6) ysize(4)
		ytitle("Coefficient on Year {fontface Cascadia Code Light:x} Baseline Uninsured Rate", size(medium) ) 
	;
	#d cr	
	graph export "${individ_figs}/govtpolicy02-ACA_eventstudy_FS.eps", replace as(eps) 

	*************************
	*** Panels B and C (RF)

	** Reduced Form 
	import delimited using "${mypath}/intermediate_csv/govtpolicy02-ACA_es_fs_ddiv.csv", clear varnames(1) asdoub
	keep if strpos(spec, "event_study")	
	keep parm outcome estimate min95 max95
	gen year = real(substr(parm, 1,4))
	drop if parm=="_cons"

	levelsof outcome	
	foreach depvar in `r(levels)' {
		preserve 
		keep if strpos(outcome, "`depvar'")
		insobs 1
		replace estimate = 0 if missing(estimate)
		replace year = 2010 if missing(year)
		replace min95 = 0 if missing(min95)
		replace max95 = 0 if missing(max95)
		sort year
		
		#d ;
		tw (connect estimate year, lcolor("8 48 107") mcolor("8 48 107") lpattern(solid)) 
			(rcap min95 max95 year, lcolor("8 48 107") lpatter(dash)), 
			xtitle("Year", size(medium)) 
			ylabel(, labsize(medium))
			xlabel(2005(1)2017, labsize(medium) nogrid ) 
			legend(off) 
			graphregion(color(white)) 
			yline(0, lcolor(gs8) lpattern(dash) lwidth(thin)) 
			xline(2014, lwidth(5.5) lc(gs12*.4) lpattern(solid))
			xsize(6) ysize(4)
			ytitle("Coefficient on Year {fontface Cascadia Code Light:x} Baseline Uninsured Rate", size(medium) ) 
		;
		#d cr	
		graph export "${individ_figs}/govtpolicy02-ACA_eventstudy_`depvar'.eps", replace
		
		restore 
	}

*********************************************************************
***  Table 4: ACA Regression Table
		
	import delimited using "${mypath}/intermediate_csv/govtpolicy02-ACA_es_fs_ddiv.csv", asdoub clear varnames(1)
	drop if strpos(spec, "event_study")
	drop min* max* indepvar n_ur
	
	replace parm = "drop_share_insured" if strpos(parm, "share_insured") & spec== "ddivdrop"
	drop spec
	gen age = "40-55"
	replace age = "44-70" if outcome == "retired_1099ssa"
	
	ren (mean_depvar sd_depvar) (mn_d sd_d)
	ren (mean_indepvar sd_indepvar) (mn_i sd_i)
	ren (estimate stderr) (es se)
	replace outcome = "_inc" if strpos(outcome, "logptotinc")
	replace outcome = "_ret" if strpos(outcome, "retired")
	replace outcome = "_sch" if strpos(outcome, "schd")
	replace outcome = "_in"  if strpos(outcome, "share_ins")
	
	* Formatting
	foreach v of varlist obs{
		tostring `v', format("%9.0fc") replace force
	}	
	foreach v of varlist es se sd* mn_*{
		replace `v' = round(`v', 0.001)
		tostring `v', format("%04.3f") replace force
		if regexm("`v'", "se") replace `v' = "(" + `v' + ")"
	}
		
	reshape wide es se mn_* sd_* obs age, i(parm) j(outcome) string
	drop if parm=="_cons"
	gen ordvar = 1
	replace ordvar = 2 if strpos(parm, "post2013")
	replace ordvar = 3 if strpos(parm, "share_ins") & !strpos(parm, "drop")
	replace ordvar = 4 if strpos(parm, "drop_share_insured")
	sort ordvar
		
	texdoc init "${tables}/aca_regression_table", replace 
	
	tex \begin{table}[h!] 
	tex \caption{\bf ACA Regression Table \label{tab:aca_estimates}} 
	tex \begin{center}
	tex \resizebox{1\textwidth}{!}{		
	tex \begin{tabular}{llccccccc}  \midrule \midrule 
	tex & & \multicolumn{1}{c}{First Stage} & \multicolumn{3}{c}{Reduced Form} & \multicolumn{3}{c}{2SLS} \\ 
	tex \cmidrule(lr){3-3} \cmidrule(lr){4-6} \cmidrule(lr){7-9} 
	tex & & (1) & (2) & (3) & (4) & (5) & (6) & (7) \\ 
	tex \midrule 
	tex \multicolumn{2}{l}{\textbf{Dependent variable:~~~~~~~~~~~~~~~~~~~~}}        ///
								  & \shortstacking{ Share         \\ Insured}       ///   
							   	  & \shortstacking{ Log           \\ Income}        ///
								  & \shortstacking{ Share with    \\ Schedule SE}   ///
								  & \shortstacking{ Share         \\ Retired}       ///
								  & \shortstacking{ Log           \\ Income}        ///
								  & \shortstacking{ Share with    \\ Schedule SE}   ///
								  & \shortstacking{ Share         \\ Retired}       \\
	tex \hline \addlinespace[2ex]
	tex \multicolumn{9}{l}{\bf Share Uninsured in 2013 ($ U_{c,2013} $)}            \\ 
	tex & ~~~~~~\$ \times \textnormal{~Years~} 2010-2013 \$  & `=es_in[1]' & `=es_inc[1]' 	& `=es_sch[1]' 	 & `=es_ret[1]'   \\ 
	tex & &                                                    `=se_in[1]' & `=se_inc[1]' 	& `=se_sch[1]' 	 & `=se_ret[1]'   \\ 
	tex \addlinespace[1ex]				  
	tex & ~~~~~~\$ \times \textnormal{~Year~} \ge 2014 \$    & `=es_in[2]' & `=es_inc[2]' 	& `=es_sch[2]' 	 & `=es_ret[2]'   \\ 
	tex & &                                                    `=se_in[2]' & `=se_inc[2]' 	& `=se_sch[2]' 	 & `=se_ret[2]'   \\ 
	tex \multicolumn{2}{l}{\bf Share Insured (\$ I_{c,t} \$)}     &&&&&      `=es_inc[3]' 	& `=es_sch[3]' 	 & `=es_ret[3]'   \\
	tex & &                                                        &&&&      `=se_inc[3]' 	& `=se_sch[3]' 	 & `=se_ret[3]'   \\
	tex \addlinespace[1ex]
	tex \multicolumn{2}{l}{Mean of Dependent Variable}         ///
		& `=mn_d_in[1]' & `=mn_d_inc[1]' & `=mn_d_sch[1]' & `=mn_d_ret[1]' & `=mn_d_inc[3]' & `=mn_d_sch[3]' & `=mn_d_ret[3]' \\
	tex \multicolumn{2}{l}{Std. Dev. of Dependent Variable}    ///
		& `=sd_d_in[1]' & `=sd_d_inc[1]' & `=sd_d_sch[1]' & `=sd_d_ret[1]' & `=sd_d_inc[3]' & `=sd_d_sch[3]' & `=sd_d_ret[3]' \\
	tex \multicolumn{2}{l}{Mean of Independent Variable}       ///
		& `=mn_i_in[1]' & `=mn_i_inc[1]' & `=mn_i_sch[1]' & `=mn_i_ret[1]' & `=mn_i_inc[3]' & `=mn_i_sch[3]' & `=mn_i_ret[3]' \\
	tex \multicolumn{2}{l}{Std. Dev. of Independent Variable}  ///
		& `=sd_i_in[1]' & `=sd_i_inc[1]' & `=sd_i_sch[1]' & `=sd_i_ret[1]' & `=sd_i_inc[3]' & `=sd_i_sch[3]' & `=sd_i_ret[3]' \\
	tex \multicolumn{2}{l}{Number of Observations}             ///
		& `=obs_in[1]' & `=obs_inc[1]' & `=obs_sch[1]'    & `=obs_ret[1]'  & `=obs_inc[3]'  & `=obs_sch[3]'  & `=obs_ret[3]'  \\
	tex \multicolumn{2}{l}{Physician Age Range}                /// 
		& `=age_in[1]' & `=age_inc[1]' & `=age_sch[1]'    & `=age_ret[1]'  & `=age_inc[3]'  & `=age_sch[3]'  & `=age_ret[3]'  \\
	
	tex  \hline \hline
	tex \end{tabular}
	tex }
	tex \end{center}
	tex \end{table}
	
	texdoc close

*********************************************************************
***  Table E.9: ACA 2SLS Regressions (Excluding 2010-2013)

	texdoc init "${tables}/aca_regression_table_drop", replace
	
	tex \begin{table}[h!] 
	tex \caption{\bf ACA 2SLS Regressions (Excluding 2010--2013) \label{tab:aca_2slsdrop}} 
	tex \begin{center}
	tex \resizebox{1\textwidth}{!}{		
	tex \begin{tabular}{llccc}  \midrule \midrule 
	tex && (1) & (2) & (3) \\ 
	tex \multicolumn{2}{l}{\textbf{Dependent variable:~~~~~~~~~~~~~~~~~~~~}}        ///
								  & \shortstacking{ Log           \\ Income}        ///
								  & \shortstacking{ Share with    \\ Schedule SE}   ///
								  & \shortstacking{ Share         \\ Retired}       \\
	tex \midrule \addlinespace[2ex]
	tex \multicolumn{2}{l}{\bf Share Insured (\$ I_{c,t} \$)}     &      `=es_inc[4]' 	& `=es_sch[4]' 	 & `=es_ret[4]'   \\
	tex & &                                                              `=se_inc[4]' 	& `=se_sch[4]' 	 & `=se_ret[4]'   \\
	tex \addlinespace[1ex]
	tex \multicolumn{2}{l}{Mean of Dependent Variable}        & `=mn_d_inc[4]' & `=mn_d_sch[4]' & `=mn_d_ret[4]' \\
	tex \multicolumn{2}{l}{Std. Dev. of Dependent Variable}   & `=sd_d_inc[4]' & `=sd_d_sch[4]' & `=sd_d_ret[4]' \\
	tex \multicolumn{2}{l}{Mean of Independent Variable}      & `=mn_i_inc[4]' & `=mn_i_sch[4]' & `=mn_i_ret[4]' \\
	tex \multicolumn{2}{l}{Std. Dev. of Independent Variable} & `=sd_i_inc[4]' & `=sd_i_sch[4]' & `=sd_i_ret[4]' \\
	tex \multicolumn{2}{l}{Number of Observations}            & `=obs_inc[4]'  & `=obs_sch[4]'  & `=obs_ret[4]'  \\
	tex \multicolumn{2}{l}{Physician Age Range}               & `=age_inc[4]'  & `=age_sch[4]'  & `=age_ret[4]'  \\
								
	tex  \hline \hline
	tex \end{tabular}
	tex }
	tex \end{center}
	tex \end{table}

*************************************************
*** Figure E.11: Geographic Variation in Share Uninsured

	* Prepare data
	* Import and save county-level data on the rate of uninsured population
	* Data source: Small Area Health Insurance Estimates Program of US Census Bureau (SAHIE)
	clear
	forvalues i = 2008/2017 {
		append using "${intermediate_data}/SAHIE/sahie_`i'.dta"
	}
	
	* Keep county-level observations for all
	* demographic groups (==everyone age <65)
	
	keep if countyfips != 0
	keep if iprcat == 0 & agecat == 0 & racecat == 0 & sexcat == 0
	rename nipr n_pop_cty_yr

	* Create cfips code
	gen county = string(countyfips, "%03.0f")
	gen state = string(statefips, "%02.0f")
	egen cfips = concat(state county)

	* Compute share uninsured and share insured in under-65 pop
	gen share_uninsured = pctelig/100
	gen share_insured = 1 - share_uninsured

	* Create treatment intensity variable - share of uninsured in 2013
	preserve
		keep if year == 2013
		keep cfips year share_uninsured
		reshape wide share_uninsured, i(cfips) j(year)
		tempfile treatment
		save "`treatment'", replace
	restore

	merge m:1 cfips using `treatment', keep(match) nogen
	drop if missing(n_pop_cty_yr)
	
	* Keep only states that exanded Medicaid in 2014 or 2015 
	* Excludes 5 states that had comprehensive early 
	* expansions from 2010 to 2014:
	* Delaware, DC, Massachusetts, New York, Vermont 
	
	forvalues x = 4/5 {
		gen expanded_201`x' = 0
	}
	
	foreach state in 04 05 06 08 09 15 17 19 21 24 26 27 32 34 35 38 39 41 44 53 54  {
		replace expanded_2014 = 1 if state == "`state'"
	}
	foreach state in 33 42 18 {
		replace expanded_2015 = 1 if state == "`state'"	
	}
	
	keep if (expanded_2014 == 1 | expanded_2015 == 1)

	keep state_name state county_name cfips year n_pop_cty_yr share_insured share_uninsured2013 
	order state_name state county_name cfips year n_pop_cty_yr share_insured share_uninsured2013 
	isid cfips year 
	sort cfips year

	* Prepare data for mapping
	destring cfips, gen(county)
	duplicates drop
	
	keep if inlist(year,2013,2017)
	bys county (year): gen uninsure_diff_2013_2017 = share_insured[_n+1] - share_insured[_n] if year == 2013
	
	keep if year == 2013
	
	merge m:1 county using "${raw_data}/maptile_geographies/county2010_database.dta", assert(2 3) nogen

	* Format graph
	format share_uninsured2013 uninsure_diff_2013_2017 %3.2f

	loc no_bins = 10
	colorpalette blues, n(`no_bins') nonumbers nograph
	loc colors "`r(p)'"
	forval i = 1/`no_bins' {
		loc lw "`lw' none"
	}
	
	* Mapping Share Uninsured Population by County, 2013
	#d ;
	spmap share_uninsured2013 using "${raw_data}/maptile_geographies/county2010_coords.dta", id(id) 
		  clnumber(10) osize(`lw') clmethod(eq)    
		  fcolor(`colors')  
		  legstyle(2) legjunction(–)  
		  legend(pos(4) symxsize(*1) symysize(*0.55) rowgap(0) size(small)
		  subtitle("Uninsured Rate", size(small))) 
		  ndfcolor(gs12) ndsize(none) ndlabel("Not included") 
	;
	#d cr 
	gr export "${individ_figs}/uninsuredmap_2013_balanced.eps", replace 

	* Mapping Difference in Share Uninsured Population by County, 2013-2017
	#d ;
	spmap uninsure_diff_2013_2017 using "${raw_data}/maptile_geographies/county2010_coords.dta", id(id) 
		  clnumber(10) osize(`lw') clmethod(eq)    
		  fcolor(`colors')  
		  legstyle(2) legjunction(–)  
		  legend(pos(4) symxsize(*1) symysize(*0.55) rowgap(*0) size(small)
		  subtitle("Change" "Insured Rate" "(2013-2017)", size(small) justification(left))) 
		  ndfcolor(gs12) ndsize(none) ndlabel("Not included") 
	;
	#d cr 
	gr export "${individ_figs}/uninsuredmap_diff_2013_2017_balanced.eps", replace 
